package derk.sam;

import java.util.*;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlock;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

public class CallDll {

	String iPort = "1001";
	String iIfOpen = "0";
	/**
	 * Convert Byte2Unicode to String
	 * @param mbyte[], bStart, bEnd
	 * @return String
	 */
	public static String libName = "sdtapi.dll";

	public static String Byte2Unicode(byte mbyte[], int bStart, int bEnd) // 不包含bEnd
	{
		StringBuffer sBuffer = new StringBuffer("");
		int i = bStart;
		while(i < bEnd )
		{
			int lbyte = mbyte[i++];

			if (lbyte < 0) lbyte += 256;

			int hbyte = mbyte[i++];

			if (hbyte < 0) hbyte += 256;

			char c = (char)((hbyte << 8) + lbyte );

			sBuffer.append(c);
		}

		return sBuffer.toString();
	}

	public static int ByteToInt(byte mbyte[])
	{
		int ret = 0;
		for(int i = 0;i < 4;i++)
		{
			ret += (mbyte[i]&0xFF) << (i*8);
		}
		return ret;
	}

	public int JCallGetBmp(FunParameter mParam[])
	{
		int nRet = 0;
		String funName = "GetBmp";

		mParam[0] = new FunParameter();
		String strPath = System.getProperty("user.dir") + "\\picture.wlt";
		mParam[0].SetParame("bytes", strPath);
		//mParam[0].SetParame("bytes", "picture.wlt");
		mParam[1] = new FunParameter();
		mParam[1].SetParame("integer", "2");

		nRet = UserCallDll("WltRS.dll", funName, mParam, 2);

		return  nRet;
	}

	public int UserCallDll(String lib, String func, FunParameter param[],int pCount)/* throws NativeException, IllegalAccessException*/ {
		// TODO Auto-generated method stub
		List<Object> retList = new ArrayList<Object>();
		int retVal = 0;

		int pos=0;
		int len=0;

		try {
			// 参数说明: lib：dll库文件名, func：函数名
			JNative jnative = new JNative(lib, func);
			jnative.setRetVal(Type.LONG);// 设置此函数的返回值


			MemoryBlock m = MemoryBlockFactory.createMemoryBlock(3000);// 在内存开辟一块空间
			Pointer pt = new Pointer(m); // 定义指针
			pt.zeroMemory();

			for (int i = 0; i < pCount; i++)
			{
				if (param[i].GetType().equals("pointer"))
				{
					jnative.setParameter(i, pt.getPointer()+pos);
					pos+= param[i].GetLen();
				}
				else if (param[i].GetType().equals("bytes"))
					jnative.setParameter(i, Type.STRING, param[i].GetValue());
				else if (param[i].GetType().equals("integer"))
					jnative.setParameter(i, Type.INT, param[i].GetValue());
				else if (param[0].GetType().equals("string"))
					jnative.setParameter(i, param[i].GetValue());
				else if (param[0].GetType().equals("long"))
					jnative.setParameter(i, Type.LONG, param[i].GetValue());
				else if (param[0].GetType().equals("bool")){
					jnative.setParameter(i, Type.VOID, param[i].GetValue());
				}
			}
			// 函数执行
			jnative.invoke();
			retVal = Integer.parseInt(jnative.getRetVal());// jnative.getRetVal();
			//m.dispose();

			pos=0;
			for (int i = 0; i < pCount; i++)
			{
				if (param[i].GetType().equals("pointer"))
				{
					len = param[i].GetLen();

					param[i].SetRetData(pt.getMemory(), pos);

					pos += len;

					retList.add(pt.getMemory());
				}
			}
			pt.dispose();
		} catch (NativeException e) {
			System.out.println("Error:" + e.getMessage());
		} catch (IllegalAccessException e) {
			System.out.println("Error:" + e.getMessage());
		}
		return retVal;
	}
}
